import { getuserhouse, getcommunity } from '../../utils/api';
import { setStorageSyncSecond, getStorageSyncTime } from "../../utils/storage"
import '../../utils/lodash_fix'
let _ = require("../../utils/lodash.min");
const to = require('../../utils/jump')
Page({

  data: {
    show: false,
    orderlist: [],
    payshow: false,
    paylist: [
      { name: '30天/ 月', num: 30 },
      { name: '90天/ 季', num: 90 },
      { name: '365天/ 年', num: 365 },
    ],
    paynav: 0,
    days: 30,
    date: '',
    house_uuid: '',
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.gettime()
    this.qurey()
  },
  qurey() {
    let a = getStorageSyncTime('community')
    if (a) {
      getcommunity().then(res => {
        if (res.statusCode == 200) {
          setStorageSyncSecond('community', res.data.data, 10)
          this.qurey()
        }
      })
    } else {
      this.getuserhouse()
    }
  },
  gettime() {
    let dataTime = ''
    let yy = new Date().getFullYear()
    let mm = new Date().getMonth() + 1
    let dd = new Date().getDate()
    let hh = new Date().getHours()
    let ff = new Date().getMinutes()
    let ss = new Date().getSeconds()
    dataTime = `${yy}-${mm}-${dd} ${hh}:${ff}:${ss}`
    this.setData({ date: dataTime })
  },
  getuserhouse() {
    getuserhouse().then(res => {
      console.log(res)
      if (_.isEqual(res.statusCode, 200)) {
        let arr = wx.getStorageSync('community')
        let list = res.data.data
        arr.forEach(e => {
          list.forEach((v) => {
            if (e.uuid == v.community_uuid) {
              v.community_name = e.community_name
              v.property_unit_price = e.property_unit_price
            }
            let a = this.data.date
            let b = v.paid_at
            v.differ = this.gettimecha(b, a)
          })
        })
        setStorageSyncSecond('houselist', list, 10)
        this.setData({ orderlist: list })
      }
    })
  },
  gettimecha(star, end) {
    var temstart = new Date(star);
    var temend = new Date(end);
    var day = (temstart - temend) / (1000 * 60 * 60 * 24)
    day = Math.ceil(day)
    return day
  },
  showpay(e) {
    wx.setStorageSync('payhouse', e.currentTarget.dataset.item)
    wx.navigateTo({
      url: '/pages/paydel/paydel',
    })
  },
  selpay(e) {
    let { i, num } = e.currentTarget.dataset
    this.setData({ paynav: i, days: num })
  },
  tobinding() {
    to('/pages/binding/binding')
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})